<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class UserorderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {     $ss=DB::table('product')->inRandomOrder()->take(6)->get()->all();
         $type=DB::table('type')->get()->all();
    	 $links=DB::table('links')->get();
        $user_id = session('homeuser')->id;
        $where = [];
        $db = DB::table('product')->leftjoin('myorder','product.id', '=', 'myorder.product_id')->where('user_id',$user_id);
         //dd($list);
         if($request->has('name')){
            // 获取要搜索的name字段的值
            $name = $request->input('name');
            //添加where模糊匹配条件
            $db->where('name','like',"%{$name}%");
            $where['name'] = $name;
        }
        // 分页
        $list = $db->paginate(3);
        //dd($list);
        //dd($list);
         return view('home.userorder',['list'=>$list,'where'=>$where,'links'=>$links,'search'=>$ss,
         'all'=>$type]);
    }
    public function interest(Request $request){
    //ajax传数据遍历收藏表，判断该商品是否收藏
        $product_id = $request->input('product_id');
        $product_id = explode(",",$product_id);
        array_pop($product_id);
         $status = $request->input('status');
        $status = explode(",",$status);
        array_pop($status);
        $id = '';
        foreach ($product_id as $k=>$v) {
            $row = DB::table('interest')->where('product_id',$v)->get();
            if($row->count()!=0){
                $id .= DB::table('myorder')->where('product_id',$v)->where('status',$status[$k])->pluck('id')[0].",";
            }
        }
    //返回商品对应的订单id
        echo json_encode($id);
    }
    //向订单表中插入数据
     public function doInterest(Request $request){
        $id = $request->input('id');
        $product_id = DB::table('myorder')->where('id',$id)->pluck('product_id');
        $user_id = session('homeuser')->id;
        $db = DB::table('interest')->insertGetId(
                ['product_id' => $product_id[0],'user_id'=>$user_id]
            );
        if($db>1){
        echo json_encode(1);
        }
    }
    public function doEval(Request $request){
        $id = $request->input('id');
        $row = DB::table('product_evaluation')->where('order_id',$id)->get();
        if($row->count()==0){
        echo json_encode(1);
        }else{
            echo json_encode(2);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       
       
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        return view('home.payorder');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {   

       
           
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id){
        $m = DB::table('myorder')->where('id',$id)->delete();
        if($m>0){
            return redirect('home/userorder');
        }else{
            return "删除失败";      
        }
    }
}
